package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.BaseInfoDao;
import cn.topevery.garden.entity.oneMap.CountVO;
import cn.topevery.garden.entity.po.BaseInfo;
import cn.topevery.garden.entity.query.BaseInfoQuery;
import cn.topevery.garden.entity.vo.BaseInfoVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class BaseInfoDaoImpl extends BaseDaoImpl<BaseInfo> implements BaseInfoDao {
    @Override
    public <P extends DbCsp> List<BaseInfo> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<BaseInfoVO> listPage(BaseInfoQuery query) {
        String table = "t_base_info tbi";

        String columns = "tbi.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tbi.c_id desc");
        sqlQueryMate
                .where(true, "tbi.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, BaseInfoVO.class);
    }

    @Override
    public BaseInfo getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<BaseInfo> getPage(BaseInfoQuery query) {
        String table = "t_base_info tbi";

        String columns = "tbi.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tbi.c_id desc");
        sqlQueryMate
                .where(true, "tbi.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, BaseInfo.class);
    }

    @Override
    public void commit(BaseInfo baseInfo, Integer userId) {
        SysUser user = UserRuntime.getSysUser();
        LocalDateTime now = LocalDateTime.now();
        if (baseInfo.getId() != null) {
//            baseMapper.updateById(baseInfo);
            saveOrUpdate(user.id.toString(), baseInfo);
        } else {
            baseInfo.setCreateId(user.id.toString());
            baseInfo.setCreateTime(now);
            baseInfo.setDbStatus(false);
            insert(user.userId, baseInfo);
        }
    }

    @Override
    public void deleteById(Integer id, Integer userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_base_info tbi");
        sqlUpdateMate.setColumnValue(userId != null, "c_db_update_id", userId)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .setColumnValue(true, "c_db_status", 1);
        sqlUpdateMate.where(id != null, "c_id", ScriptConditionEnum.Eq, id);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
        });
    }

    @Override
    public List<CountVO> getSurvey(List<String> types) {
        StringBuilder strBuilder = new StringBuilder();
        for (String type : types) {
            strBuilder.append(",").append("'").append(type).append("'");
        }
        String substring = strBuilder.substring(1);
        String columns = "c_component_type AS name, COUNT(c_component_type)  AS value ";
        String table = " t_park_component \n" +
                "WHERE c_component_type IN (" + substring + ")\n" +
                "GROUP BY c_component_type";
        SqlQueryMate queryMate = new SqlQueryMate(table).setColumns(columns);
        return selectList(queryMate, CountVO.class);
    }
}
